//
//  ZSY_AllEuropeViewController.m
//  ZSY_BreadTrip
//
//  Created by 张诗宇 on 16/1/4.
//  Copyright © 2016年 张诗宇. All rights reserved.
//

#import "ZSY_MoreListViewController.h"
#import "ZSY_DestinationModel.h"
#import "ZSY_MoreListCollectionViewCell.h"
#import "ZSY_DestinationDetailViewController.h"

@interface ZSY_MoreListViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, retain) ZSY_CollectionView *europeCollectionView;
@property (nonatomic, retain) ZSY_MoreListCollectionViewCell *europeCell;
@property (nonatomic, retain) NSMutableArray *allArray;

@end

@implementation ZSY_MoreListViewController

- (void)dealloc {
    [_europeCell release];
    [_flowLayout release];
    [_europeCollectionView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor whiteColor];
    
    self.allArray = [NSMutableArray array];
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = CGSizeMake((WIDTH - 30) / 2, 180);
    self.flowLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
    
    CGRect frame = self.view.frame;
    frame.size.height -= 114;
    self.europeCollectionView = [[ZSY_CollectionView alloc] initWithFrame:frame collectionViewLayout:_flowLayout];
    self.europeCollectionView.delegate = self;
    self.europeCollectionView.dataSource = self;
//    self.europeCollectionView.backgroundColor = [UIColor clearColor];
    
    [self.europeCollectionView registerClass:[ZSY_MoreListCollectionViewCell class] forCellWithReuseIdentifier:@"europeCell"];
    [self.view addSubview:_europeCollectionView];
    
    [ZSY_AppTools creatingDataTask:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/index_places/%@/", self.index] parameters:nil Cookie:nil User_Agent:nil successAFNBlock:^(id resuposeObject) {
        NSDictionary *dataDic = resuposeObject;
        NSArray *array = [dataDic objectForKey:@"data"];
        for (NSDictionary *dic in array) {
            ZSY_DestinationModel *model = [[ZSY_DestinationModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            NSLog(@"name =  %@", model.name_orig);
            [self.allArray addObject:model];
            [model release];
        }
        [self.europeCollectionView reloadData];
        
    }];
    
    [_europeCollectionView release];
    [_flowLayout release];
    
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    self.europeCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"europeCell" forIndexPath:indexPath];
    ZSY_DestinationModel *model = [_allArray objectAtIndex:indexPath.row];
    _europeCell.model = model;
    
    return _europeCell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _allArray.count;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ZSY_DestinationDetailViewController *desDetailVC = [[ZSY_DestinationDetailViewController alloc] init];
    ZSY_DestinationModel *model = [_allArray objectAtIndex:indexPath.row];
    desDetailVC.url = model.url;
    [self.navigationController pushViewController:desDetailVC animated:YES];
    [desDetailVC release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
